package com.api.phm.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.api.common.utils.DateUtils;
import com.api.phm.domain.vo.PhmDevicesScoreVo;
import com.api.phm.mapper.OtherCommonMapper;
import com.api.phm.service.IThreeDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ThreeDServiceImpl implements IThreeDService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private OtherCommonMapper otherCommonMapper;

    @Override
    public List<Map<String, Object>> threeDData(PhmDevicesScoreVo vo) {
        List<Map<String, Object>> rList = new ArrayList<>();
        Query query = new Query();

        query.addCriteria(
                        new Criteria().andOperator(
                                Criteria.where("dateTime").gte(vo.getStartTime()),
                                Criteria.where("dateTime").lte(vo.getEndTime())
                        ))
                .addCriteria(Criteria.where("robotId").is(vo.getRobotId()))
                .addCriteria(Criteria.where("elementId").is(vo.getElementId()))
                .addCriteria(Criteria.where("datCol").is(vo.getDatCol()));

        List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, "takt_list");
//        List<DevicesMOGDBQuery> list = mongoTemplate.find(query, DevicesMOGDBQuery.class, "takt_list");

        list.forEach(i -> {
            Map<String, Object> map = new HashMap<>();
            map.put("time", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addHours(
                    DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, i.getString("dateTime"))
                    , -8)));
            List<String> taktJa = Arrays.asList(i.getJSONObject("taskData").getString("takt").split(","));
            map.put("taktArr", taktJa.stream().mapToDouble(Double::valueOf));
            rList.add(map);
        });
        return rList;
    }

    @Override
    public List<Map<String, Object>> threeDPtnData(PhmDevicesScoreVo vo) {
        return otherCommonMapper.threeDPtnDataList(vo);
    }
}
